import Vue from 'vue'
import jsCookie from 'js-cookie'
// 活动
import * as activities from './action/activity'
// app 升级
import * as apps from './action/app'
// 行为统计
import * as behaviors from './action/behavior'
// 存股宝
import * as cgbs from './action/cgb'
// 广告
import * as ads from './action/gao'
// 虚拟币
import * as golds from './action/gold'
// 用户邀请关系
import * as invitations from './action/invitation'
// 组合
import * as portfolios from './action/portfolio'
// 组合排行榜审核
import * as ranks from './action/rank'
// 推广员系统
import * as promoters from './action/promoter'
// 统计报表
import * as statement from './action/statement'
// 邀请好友活动
import * as invite from './action/invite-activity'
// 智能助手
import * as AI from './action/search'
// app 内容管理
import * as content from './action/app-content'
// 投研大咖说
import * as castTalk from './action/cast-talk'
// 专家库
import * as expert from './action/expert'
// 港股
import * as ganggu from './action/ganggu'
// 公告
import * as announcement from './action/announcement'
// 邀请好友免佣
import * as inviteS4 from './action/invite-s4'
// 收费课程
import * as classes from './action/classes'
// 收费课程
import * as risk from './action/risk'

const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage'

// 更新 state 状态
const updateState = ({ dispatch }, mutation, value) => {
  dispatch(mutation, value)
}

// 设置使用的模块菜单
const setMenuType = ({ dispatch }, type) => {
  dispatch('SET_MENU_TYPE', type)
}

// 发送短信验证码
const sendSmsResource = Vue.resource(server.root + '/manage/view/new/login/sendVerifySms')
const sendSms = ({ dispatch }, params) => {
  return sendSmsResource.get(params).then(res => {
    if (+res.data.code === SUCCESS) {
      return dispatch('SEND_SMS_DISABLED', true)
    }
    return Promise.reject(res.data)
  })
}

// 登陆
const loginResource = Vue.resource(server.root + '/manage/view/new/ajax/login')
const login = ({ dispatch }, params) => {
  return loginResource.save(params).then(res => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve()
    }
    return Promise.reject(res.data)
  })
}

const logoutResource = Vue.resource(API_ROOT + '/user/logout')
const logout = ({ dispatch }) => {
  return logoutResource.save().then(res => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve()
    }
    return Promise.reject(res.data)
  })
}

// 显示弹窗消息
const showMsg = ({ dispatch }, params) => {
  if (params.content && params.content.indexOf('先登录') > -1) {
    jsCookie.remove(SESSION_COOKIE_NAME)
    setTimeout(() => {
      if (window.top !== window) {
        window.top.location.href = client.htmlPath + '/login.html'
      } else {
        window.location.href = client.htmlPath + '/login.html'
      }
    }, 3000)
  }
  dispatch('SET_ALERT_MSG', params)
}

// 获取菜单
const menuListResource = Vue.resource(server.root + '/manage/view/new/tree/node')
const getMenuList = ({ dispatch }) => {
  return menuListResource.get().then(res => {
    dispatch('SET_MENU_LIST', res.data)
    return Promise.resolve()
  })
}

export default {
  updateState,
  setMenuType,
  sendSms,
  login,
  logout,
  showMsg,
  getMenuList,
  ...activities,
  ...apps,
  ...behaviors,
  ...cgbs,
  ...ads,
  ...golds,
  ...invitations,
  ...portfolios,
  ...ranks,
  ...promoters,
  ...statement,
  ...invite,
  ...AI,
  ...content,
  ...castTalk,
  ...expert,
  ...ganggu,
  ...announcement,
  ...inviteS4,
  ...classes,
  ...risk
}
